/**
 * 顶部导航菜单
 */
import tem from './index.html'
import $_fn from '~plugins'
class navtopCtrl {
  constructor($scope,$rootScope,$location,$interval,$state,http) {
      $scope.curUrl = $location.path()
      $scope.roles =  $_fn.getLocal('userHelp')
      $scope.Menu = $_fn.getLocal('menu')
      //一级菜单跳转
      $scope.goView = function(url,path,item){
          var curRoute = $scope.Menu.find(li => li.state === url)
          $_fn.setLocal('parentMenu',curRoute)
          if(url !=='index'){
              console.log(curRoute)
              console.log('ryy-route:', url, curRoute.children[0].state)

              let _param = {}
              if(item){
                _param = item
              }
              $state.go(url+'.'+ curRoute.children[0].state, _param, {reload:true})//reload为true是不缓存页面，每次都加载
          } else {
              $state.go(url,{},{reload:true})
          }
          $rootScope._curUrl = path
      }
      //一级菜单高亮
      $scope.isActive = function(cur){
        if($scope.curUrl.indexOf(cur) < 0){
          return false
        }else{
          return true
        }
      }
      $scope.exitLogin=function(){
          http.post('loginOut.do',function(data){
              if(data.errcode > 0){
                  $rootScope.$apply(function(){
                      $location.path('/login')
                      $interval.cancel($_fn.mess)
                  })
                  $_fn.removeLocal('userHelp')
                  $_fn.removeLocal('menu')
                  $_fn.removeLocal('parentMenu')
              }
          })
      }
  }
}


navtopCtrl.$inject = ['$scope','$rootScope','$location','$interval','$state','http']
module.exports = angular.module('directive.navTop', []).directive('navTop', function() {
    return {
        template: tem,
        replace:true,
        controller: navtopCtrl,
        link:function(scope){
            scope.HbHr = $_fn.getLocal('HbHr')
        }
    }
}).name